//
//  EPMRefreshControl.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/4/1.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMRefreshControl.h"
typedef NS_ENUM(NSInteger, EPMRefreshControlType) {
    Normal,
    Pulling,
    Refreshing,
};


@interface EPMRefreshControl()
@property (weak,nonatomic) UILabel* titleLabel;
@property (weak,nonatomic) UIImageView* imgView;
@property (weak,nonatomic) UIActivityIndicatorView* indicator;
@property (weak,nonatomic) UIScrollView* scrollView;
@property (assign,nonatomic) EPMRefreshControlType type;
@end
@implementation EPMRefreshControl
- (instancetype)initWithFrame:(CGRect)frame{
    CGRect rect = CGRectMake(0, -50, [UIScreen mainScreen].bounds.size.width, 50);
    self.backgroundColor = [UIColor redColor];
    if (self = [super initWithFrame:rect]){
        [self setupUI];
    }
    return self;
}
- (void)endRefreshing{
    self.type = Normal;
}

- (void)setType:(EPMRefreshControlType)type
{
    EPMRefreshControlType oldType = _type;
    _type = type;
    
    switch (type) {
        case Normal:{
            self.imgView.hidden = NO;
            if (oldType == Refreshing){
                self.titleLabel.text = @"正常";
                [UIView animateWithDuration:0.25 animations:^{
                    UIEdgeInsets inset = self.scrollView.contentInset;
                    inset.top -= 50;
                    self.scrollView.contentInset =  inset;
                } completion:^(BOOL finished) {
                  
                    self.imgView.hidden = NO;
                }];
            }
            [UIView animateWithDuration:0.25 animations:^{
                self.imgView.transform = CGAffineTransformIdentity;
            }];
        }
            break;
        case Pulling:{
            self.titleLabel.text = @"下拉中";
            [UIView animateWithDuration:0.25 animations:^{
                self.imgView.transform = CGAffineTransformMakeRotation(-3*M_PI);
            }];
        }
            break;
        case Refreshing:{
            self.titleLabel.text = @"刷新中";
            [UIView animateWithDuration:0.25 animations:^{
                self.imgView.hidden = true;
                [self.indicator startAnimating];
                UIEdgeInsets inset = self.scrollView.contentInset;
                inset.top += 50;
                self.scrollView.contentInset =  inset;
                
            } completion:^(BOOL finished) {
                [self sendActionsForControlEvents:UIControlEventValueChanged];
            }];
            
        }
            break;
        default:
            break;
    }
    
    
    
    
}


- (void)setupUI{
    UILabel* titleLabel = [UILabel EPM_Title:@"正常" textColor:[UIColor orangeColor] fontSize:15];
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    UIImageView* imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tableview_pull_refresh"]];

    [self addSubview:imgView];
    self.imgView = imgView;
    
    UIActivityIndicatorView* indicator = [UIActivityIndicatorView new];
    indicator.color = [UIColor orangeColor];
    [self addSubview:indicator];
    self.indicator = indicator;
    
    
    titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  
    
 [[NSLayoutConstraint constraintWithItem:titleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0] setActive:YES];
   [[NSLayoutConstraint constraintWithItem:titleLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0] setActive:YES];
    
    imgView.translatesAutoresizingMaskIntoConstraints = NO;
   [[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:titleLabel attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]setActive:YES];
   [[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:titleLabel attribute:NSLayoutAttributeLeading multiplier:1.0 constant:-5]setActive:YES];
    
    indicator.translatesAutoresizingMaskIntoConstraints = NO;
    [[NSLayoutConstraint constraintWithItem: indicator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:titleLabel attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]setActive:YES];
    [[NSLayoutConstraint constraintWithItem: indicator attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:titleLabel attribute:NSLayoutAttributeLeading multiplier:1.0 constant:-5]setActive:YES];
    
}

#pragma
#pragma-监听方法
- (void)willMoveToSuperview:(UIView *)newSuperview{
    self.scrollView = (UIScrollView*)newSuperview;
    [_scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    CGFloat ContenstOffsetMaxY = -50 - self.scrollView.contentInset.top;
    CGFloat ContentOffsetY = self.scrollView.contentOffset.y;
    
//    NSLog(@"%f %ld",ContentOffsetY,(long)_type);
    if (self.scrollView.dragging == true){
        if (ContentOffsetY >= ContenstOffsetMaxY && self.type == Pulling){
            self.type = Normal;
        }else if (ContentOffsetY < ContenstOffsetMaxY && self.type == Normal){
            self.type = Pulling;
        }
        
    }else{
        if (self.type == Pulling){
            self.type = Refreshing;
        }
    }
}

- (void)dealloc{
    
    [self.scrollView removeObserver:self forKeyPath:@"contentOffset"];
    
}



@end
